<{include file="admin/header.html"}>
<{FRM_XOAD::getClientCode()}>

<script type="text/javascript">

var cur_edited_value = "";
var cur_id = "";
var reInteger = /^\d+$/;

function isInteger(s)
{   
	var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    return reInteger.test(s)
}

function removeLeadingZero(number_string)
{	
	if ((number_string.length == 1 && number_string == 0)) {
		return parseInt(number_string);
	} else {
		while(number_string[0] == '0' && number_string.length > 1
			  && number_string[1] != '.') {
			number_string = number_string.substr(1, number_string.length -1);
		} 
	 return number_string;	
	}
}

/*
* Validate before updating
* param: cur_object: ID of object contains the value should be checked
* param: param_type: PARAM_TYPE_DATETIME or PARAM_TYPE_NUMBER
* 
*/
function validate(cur_object, param_type)
{	
	// check whether or not the object's value is empty
	if(isEmptyTextBox(cur_object, '<{#_LB_MUST_NOT_BE_EMPTY#|utf8_escape_js}>')) {
		return false;
	}
	
	// check validation belong to param_type: DATETIME or NUMBER
	if(param_type == <{Parameter::PARAM_TYPE_DATETIME}>){
		// check validation for DATETIME
		if(!isValidDate(cur_object.value, "DMY")){
			// show bubble message
			showErrorBubble(cur_object, '<{#_LB_DATE_INVALID#|utf8_escape_js}>');
			return false;
		}
	}else if(param_type == <{Parameter::PARAM_TYPE_NUMBER}>){
		// check validation for NUMBER, equal or greater than 0
		if(!isPositiveWholeInteger(cur_object.value)){
			// show bubble message
			showErrorBubble(cur_object, '<{#_MSG_INVALID_NUMBER#|utf8_escape_js}>');
			return false;
		}
	}else if(param_type == <{Parameter::PARAM_TYPE_POSITIVE_NUMBER}>){ 
		// check validation for NUMBER, greater than 0
		if(!isPositiveWholeInteger(cur_object.value) || cur_object.value <= 0){
			// show bubble message
			showErrorBubble(cur_object, '<{#_MSG_INVALID_NUMBER#|utf8_escape_js}>');
			return false;
		}
	} else if(param_type == <{Parameter::PARAM_TYPE_DOUBLE_NUMBER}>){
		// check validation for FLOAT NUMBER 
		// if(!isNumeric(cur_object.value) 
		
		if(!isNumeric(cur_object.value)){
			// show bubble message
			showErrorBubble(cur_object, '<{#_MSG_INVALID_NUMBER#|utf8_escape_js}>');
			return false;
		}
	}

	return true;
}

function showbox(id)
{
	//alert("S: curid = " + cur_id + " ---- cur_value = " + cur_edited_value);
	var box = document.getElementById("box_"+id);	
	var text = document.getElementById("text_"+id);
	var text_value = document.getElementById("value_"+id);
	if( cur_id != "" && cur_id != id){
		hidebox(cur_id);
	}
	cur_edited_value = text_value.value;
	cur_id = id;
	box.style.display = "none";	
	text.style.display = "";
}

function hidebox(id)
{
	//alert("H: curid = " + cur_id + " ---- cur_value = " + cur_edited_value);
	var box = document.getElementById("box_"+id);
	var text = document.getElementById("text_"+id);
	var text_value = document.getElementById("value_"+id);
	//alert(text_value.value + " --- " + cur_edited_value);
	if(cur_id == id){
		text_value.value = cur_edited_value;
	}

	// alert(cur_edited_value);
	//box.value = cur_edited_value;
	//text.value = cur_edited_value;
	//text_value.value = cur_edited_value;
	box.style.display = "";
	text.style.display = "none";		
	
}

function save_value(id, param_type)
{
	var val = document.getElementById("value_"+id);
	var args = new Array();
	
	// check validation
	if(validate(val, param_type)){
		var param_value = '';
		//args.push(id);
		//args.push(Number(val.value));
		if (isValidDate(val.value, "DMY")) {
			param_value = val.value;
		}
		else { 
				param_value = removeLeadingZero(val.value);
		} 
		if (document.getElementById("box_value_"+id).innerHTML != param_value)
		{	
			if(server.saveParam(id, param_value))
			{   
				if(val.type == 'password'){
					var str = '';
					for(var i = 0; i < param_value.length; i++){
						str += '*';
					}
					document.getElementById("box_value_"+id).innerHTML = str;
				}else{
					document.getElementById("box_value_"+id).innerHTML = param_value;
				}
			}
		}
		cur_edited_value = param_value;
		hidebox(id);
	}
}

function switch_checkbox(id)
{
	var text = document.getElementById("text_"+id);
	
	if (text.style.display == "none")
		text.style.display = "";		
	else
		text.style.display = "none";	
}

function save_checkbox(id)
{
	var val = document.getElementById('value_'+id);
	var args = new Array();
	var chk_value=0;
	args.push(id);	
	if (val.checked)
	{
		args.push(1);	
		chk_value = 1;
	}
	else
	{
		args.push(0);	
		chk_value = 0;
	}
	server.saveParam(id, chk_value);
	switch_checkbox(id);
}

function editTextValue(id)
{
	var text_value = document.getElementById("value_"+id);
	text_value.disabled = false;
	text_value.className = "";
	cur_edited_value = text_value.value;
	cur_id = id;
	
	var box = document.getElementById("box_"+id);
	box.style.display = "none";	
	
	var text = document.getElementById("text_"+id);
	text.style.display = "";
}

function disableTextEdit(id)
{
	var text_value = document.getElementById("value_"+id);
	text_value.disabled = true;
	text_value.className = "disabled";

	var text = document.getElementById("text_"+id);
	text.style.display = "none";					
			
	var box = document.getElementById("box_"+id);
	box.style.display = "";
	hidebox(id); 
}
</script>
<div class="tabs">
	<div  class="panel-header-select" >	
		<label><{#_LB_CONFIG#}></label>
	</div>
</div>



<table class="grid" width="100%">				
	<tr class="head">
		<td width="50%"><{#_LB_NAME#|utf8_escape_html}></td>
		<td width="50%">&nbsp;</td>				
	</tr>
	<{foreach key=paramkey item=param from=$params}>
			<tr>
			<td><{$param.name}></td>
			
			<td>
				<div align="right">
				<{if $param.type == Parameter::PARAM_TYPE_BOOLEAN}>		
					<div id="box_<{$param.id}>">								
						<input class="no_border" onclick="switch_checkbox(<{$param.id}>)" type="checkbox" name="value_<{$param.id}>" id="value_<{$param.id}>" <{if $param.value }>checked<{/if}> />		
						<a id="text_<{$param.id}>" style="display:none" href="javascript:save_checkbox(<{$param.id}>)" >
							<img border=0  alt="done_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/done_small.png"/>
						</a>	
					</div>
				<{elseif $param.type == Parameter::PARAM_TYPE_DATETIME}>	
					<div id="box_<{$param.id}>">
						<div style="display:inline" id="box_value_<{$param.id}>"><{$param.value}></div>
						<a href="javascript:showbox(<{$param.id}>)" >
							<img border=0  alt="edit_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/edit_small.png"/>
						</a>								
					</div>
					<div id="text_<{$param.id}>" style="display:none">
						<input id="value_<{$param.id}>" name="value_<{$param.id}>" value="<{$param.value}>" type="text" size="60" maxlength="10" />
						<a onclick="javascript:displayCalendar(document.getElementById('value_<{$param.id}>'),'dd/mm/yyyy',document.getElementById('value_<{$param.id}>'));" href="javascript:void(0)" style="text-decoration: none;">
							<img border=0  alt="calendar_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/calendar_small.png"/>
						</a>
						<a href="javascript:save_value(<{$param.id}>,<{$param.type}>);" >
							<img border=0 alt="done_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/done_small.png"/>
						</a>
						<a href="javascript:hidebox(<{$param.id}>);" >
							<img border=0 alt="delete_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/delete_small.png"/>
						</a>									
					</div>
				<{elseif $param.type == Parameter::PARAM_TYPE_PASSWORD}>
					<div id="box_<{$param.id}>">
						<div style="display:inline" id="box_value_<{$param.id}>"><{$param.value}></div>					
						<a href="javascript:showbox(<{$param.id}>)" >
							<img border=0 alt="edit_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/edit_small.png"/>						
						</a>												
					</div>
					<div id="text_<{$param.id}>" style="display:none">
						<input id="value_<{$param.id}>" name="value_<{$param.id}>" value="<{$param.value}>" type="text" size="60"/>
						<a href="javascript:save_value(<{$param.id}>,<{$param.type}>);" >
							<img border=0 alt="done_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/done_small.png"/>						
						</a>
						<a href="javascript:hidebox(<{$param.id}>);" >
							<img border=0 alt="delete_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/delete_small.png"/>						
						</a>
					</div>
				<{else}>
					<div id="box_<{$param.id}>">
						<div style="display:inline" id="box_value_<{$param.id}>"><{$param.value}></div>					
						<a href="javascript:showbox(<{$param.id}>)" >
							<img border=0 alt="edit_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/edit_small.png"/>						
						</a>												
					</div>
					<div id="text_<{$param.id}>" style="display:none">
						<input id="value_<{$param.id}>" name="value_<{$param.id}>" value="<{$param.value}>" type="text" size="60"/>
						<a href="javascript:save_value(<{$param.id}>,<{$param.type}>);" >
							<img border=0 alt="done_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/done_small.png"/>						
						</a>
						<a href="javascript:hidebox(<{$param.id}>);" >
							<img border=0 alt="delete_small.png" src="<{$smarty.const.STATIC_COMPONENT_URL}>images/delete_small.png"/>						
						</a>
					</div>
				<{/if}>
				</div>
			</td>
		</tr>
   <{/foreach}>
</table>
		
<{include file="admin/footer.html"}>
